package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsEstimatedArrivalOrder;
import com.jy.wms.pojo.WmsLibraryPosition;
import com.jy.wms.pojo.WmsPalletSorting;
import com.jy.wms.service.WmsPalletSortingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsPalletSortingController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author ln
 * @Date Mon Sep 25 17:10:00 CST 2017
 */
@Controller
@RequestMapping("/wmspalletsorting")
@Api(value = "/wmspalletsorting", description = "码盘操作")
public class WmsPalletSortingController extends BaseController<WmsPalletSortingService,WmsPalletSorting> {

    @ApiOperation(value = "码盘 POST请求查询ASN单据",response = JSONObject.class, notes = "POST请求查询ASN单据信息")
    @PostMapping(value = "/list")
    public  @ResponseBody JSONObject getSysUser(@RequestBody WmsEstimatedArrivalOrder entity, HttpSession session) throws Exception {
        Page<WmsEstimatedArrivalOrder> list=new Page<WmsEstimatedArrivalOrder>();
        JSONObject json = getJSONObject();
        try {
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            entity.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size()>0){
                entity.setHzid(hz.get(0).getHzid());
                list= baseService.baseSelectPage(entity);
                for (int i=0;i<list.size();i++){
                    //获取仓库名称
                    list.get(i).setCkmc(String.valueOf(userinfo.getUser().getOrgName()));
                }
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }

    /*
 * 码盘(批量插入)
 * 1.查询单据 查询明细 多条单据查询
 * 2.码盘 判断是否全部码盘
 * 3.全部码盘添加码盘数据 修改单据码盘状态
 */
    @ApiOperation(value = "码盘 新增码盘",response = JSONObject.class, notes = "POST新增码盘")
        @PostMapping(value = "add")
        public  @ResponseBody JSONObject addWmsPalletSorting(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel, HttpSession session) throws Exception {
            JSONObject json=new JSONObject();
        List<String> list=new ArrayList<String>();
        try{
            // 获取user
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            //获取仓库id
            // entity.setCkid,(userinfo.getUser().getRentId().toString());
           /* for (WmsPalletSorting list1:list) {
                list1.setCkid(String.valueOf(userinfo.getUser().getOrgId().get(0)));
            }*/
           String ckid= String.valueOf(userinfo.getUser().getOrgId().get(0));
            List<SysHz> hz=(List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid=hz.get(0).getHzid();
            //根据货主id 查询上架名称
            String shelf=baseService.shelf(hzid);
            //库位高度不同,空库位优先
            String sjzzgz= TurnoverRules.sjzzgz2.getType();
            if(shelf.equals(sjzzgz)){
                list=baseService.baseInsertSelective(setModel.getIds(),ckid,hzid); //002
            }else{
                list=baseService.baseInsertPlSelective(setModel.getIds(),ckid); //001 003 004
            }
            if (list.size()==0){
                json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增码盘成功","", "");
            }else if (list.size()>0){
                int cg=setModel.getIds().size()-list.size();
                int sb=list.size();
                json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"预期数量与码盘数量不相同","",JSONArray.fromObject(list).toString());
            }
        } catch(ServiceException e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"新增码盘失败",e.getMessage(),JSONArray.fromObject(list).toString());
        }catch(Exception e){
            e.printStackTrace();
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增码盘失败",e.getMessage(),"");

        }
        return json;
    }

    /**
     * 码盘查询(查询码盘明细)
     */
//    @ApiOperation(value = "码盘 POST请求查询码盘明细",response = JSONObject.class, notes = "POST请求查询码盘明细")
//    @PostMapping(value = "/baseSelectMx/{ssyqdid}")
//    public  @ResponseBody JSONObject baseSelectMx(@PathVariable String ssyqdid, @RequestBody Map<String,Integer> map) throws Exception {
//        Page<WmsPalletSorting> list=new Page<>();
//        JSONObject json = getJSONObject();
//        try {
//            list=baseService.baseSelectMx(ssyqdid,map.get("page"),map.get("rows"));
//            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
//                    ,(int) list.getTotal());
//        }catch (Exception e){
//            e.printStackTrace();
//            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"查询码盘明细失败",e.getMessage(),"");
//        }
//        return json;
//    }

    /**
     * 码盘查询(查询码盘明细)
     */
    @ApiOperation(value = "码盘 POST请求查询码盘明细",response = JSONObject.class, notes = "POST请求查询码盘明细")
    @PostMapping(value = "/baseSelectMx")
    public  @ResponseBody JSONObject baseSelectMx(@RequestBody Map<String,Object> map) throws Exception {
        Page<WmsPalletSorting> list=new Page<>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.baseSelectMx(map.get("ssyqdid").toString(),(int)map.get("page"),(int)map.get("rows"));
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.getTotal());
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"查询码盘明细失败",e.getMessage(),"");
        }
        return json;
    }
    /**
     * 根据勾选的码盘明细打印
     */

    @ApiOperation(value = "勾选码盘明细打印", response = JSONObject.class, notes = "勾选码盘明细打印")
    @PostMapping(value = "baseDyid")
    public  @ResponseBody JSONObject baseDyid(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
        List<WmsPalletSorting> list=new ArrayList<WmsPalletSorting>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.baseDyid(setModel.getIds());
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.size());
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"码盘明细打印失败",e.getMessage(),"");
        }
        return json;
    }

    /**
     * 打印托盘码
     */

    @ApiOperation(value = "打印托盘码", response = JSONObject.class, notes = "打印托盘码")
    @PostMapping(value = "dySelecttp")
    public @ResponseBody JSONObject dySelecttp(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
        List<WmsPalletSorting> list=new ArrayList<WmsPalletSorting>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.dySelecttp(setModel.getIds());
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString()
                    ,(int) list.size());
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"打印托盘码失败",e.getMessage(),"");
        }
        return json;
    }

    @ApiOperation(value = "取消盘码", response = JSONObject.class, notes = "取消盘码，ids:选中的多条预期到货通知单id集合")
    @PostMapping(value = "delete")
    public  @ResponseBody JSONObject deleteWmsPurchaseOrder(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
        //SysUser user=(SysUser)getSessionInfo(SessionInfo.SessionName);
        try {
            baseService.baseDeleteBathPrimaryKeys(setModel.getIds(), "0");
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "取消盘码成功", "取消盘码", "");

        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "取消盘码失败", e.getMessage(), "");

        }catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "取消盘码失败", "取消盘码", "");
        }
    }

  /*  @ApiOperation(value = "码盘收货 POST请求查询码盘信息",response = JSONObject.class, notes = "POST请求查询码盘信息")
    @PostMapping(value="/getMpmx/{ssyqdid}")
    public @ResponseBody JSONObject getWmsOwnerOrdertype(@PathVariable String ssyqdid, @RequestBody Map<String,Integer> map, HttpSession session){
        Page<WmsPalletSorting> list = new Page<WmsPalletSorting>();
        JSONObject json = getJSONObject();
        try {
            list= baseService.baseSelectBySsyqdid(ssyqdid,map.get("page"),map.get("rows"));
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"码盘收货信息失败","查询码盘收货信息失败","");
        }
        return json;
    }*/
  /*
   * 2018-8-2
   * 码盘收货之确认收货
   * */
  @ApiOperation(value = "码盘收货 POST请求查询码盘信息",response = JSONObject.class, notes = "POST请求查询码盘信息")
  @PostMapping(value="/getMpmx")
  public @ResponseBody JSONObject getWmsOwnerOrdertype(String ssyqdid, @RequestBody Map<String,Integer> map,HttpSession session){
      Page<WmsPalletSorting> list = new Page<WmsPalletSorting>();
      JSONObject json = getJSONObject();
      try {
          list= baseService.baseSelectBySsyqdid(ssyqdid,map.get("page"),map.get("rows"));
          json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
      }catch (Exception e) {
          e.printStackTrace();
          json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"码盘收货信息失败","查询码盘收货信息失败","");
      }
      return json;
  }
    @ApiOperation(value = "码盘收货 生成托盘码",response = JSONObject.class, notes = "GET生成托盘码")
    @PostMapping(value = "/addLPN")
    public @ResponseBody  JSONObject addLPN() {
        try {
            String lpn= baseService.selectlpnBylpn();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"生成托盘码成功","生成托盘码",lpn);

        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(),"生成托盘码失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"生成托盘码",e.getMessage(),"");
        }
    }
    /*
     * 码盘收货 调整按钮
     * 预修改查询
     * */
    @PostMapping(value = "/getFindMaPan")
    @ApiOperation(value = "码盘信息 查看码盘信息", response = JSONObject.class, notes = "Post查看码盘信息")
    public @ResponseBody
    JSONObject getFindMaPan(@RequestBody SetAndModifyModel setModel) {
        JSONObject json = new JSONObject();
        WmsPalletSorting entity = new WmsPalletSorting();
        try {
            entity = baseService.getFindMaPan(setModel.getId());
            JSONObject jsonObject3 = new JSONObject();
            if(entity.getKwid()!=null&& entity.getKwmc()!="") {
                jsonObject3.accumulate("id", entity.getKwid());
                jsonObject3.accumulate("text", entity.getKwmc());
            }else{
                jsonObject3.accumulate("id", "");
                jsonObject3.accumulate("text", "");
            }
            String str= com.alibaba.fastjson.JSONObject.toJSONString(entity);
            json=JSONObject.fromObject(str);
            json.accumulate("telephone",jsonObject3);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    /*
     * select2查询库位
     * 码盘收货
     * */
    @PostMapping(value = "getKwBymc")
    @ApiOperation(value = "码盘收货 查看库位ID",response = JSONObject.class, notes = "查看库位ID")
    public @ResponseBody JSONObject getKwBymc(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json =getJSONObject();
        try {
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String owner=null;
            List<SysHz> hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                owner=hz.get(0).getHzid();
            }
            String ckid=String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String mc="";
            if(map.containsKey("mc")){
                mc=map.get("mc").toString();
            }
            String lx="4";

            List<WmsLibraryPosition> list=baseService.searchKwInfo(mc,lx,owner,ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"查询库位失败",e.getMessage(),"");
        }
        return json;
    }


}
